package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Category;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Responser;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Requester;

/**
 * Created by Max on 30.03.2014.
 */
public class MenuItemContainer extends VerticalPanel implements
        Responser,HasSelectionHandlers<MenuItemBox> {

    private PickupDragController widgetDragController;

    public MenuItemContainer(AbsolutePanel dragSpace) {
        this.addStyleName("menu-container");
        // initialize a widget drag controller
        this.widgetDragController = new PickupDragController(dragSpace, false);
        this.widgetDragController.setBehaviorMultipleSelection(false);
        // initialize a widget drop controller
        MenuItemDropController menuItemDropController = new MenuItemDropController(this);
        // register drop controller
        this.widgetDragController.registerDropController(menuItemDropController);
    }

    @Override
    public void handleGetResponse(JSONArray data) {
        this.setLiveData(data);
    }
    @Override
    public void handlePostResponse(int responseCode) {
    }

    @Override
    public HandlerRegistration addSelectionHandler(SelectionHandler<MenuItemBox> handler) {
        return super.addHandler(handler, SelectionEvent.getType());
    }

    public void setBoxSelectedByMenuId(int menuId) {
        for (Widget box : this.getChildren()) {
            if (box instanceof MenuItemBox) {
                MenuItemBox miBox = (MenuItemBox)box;
                if (miBox.getMenuId()==menuId) {
                    this.setBoxSelected(miBox);
                }
            }
        }
    }

    protected void setBoxSelected(MenuItemBox newSelectedBox) {
        for (Widget box : this.getChildren()) {
            if (box instanceof MenuItemBox) {
                MenuItemBox miBox = (MenuItemBox)box;
                if (miBox.isSelected()) {
                    miBox.setSelected(false);
                }
            }
        }
        newSelectedBox.setSelected(true);
        SelectionEvent.fire(this,newSelectedBox);
    }

    public void setLiveData(JSONArray data) {
        int n = data.size();
        this.clear();
        if (n > 0) {
            for(int i = 0; i < n; i++) {
                JSONObject obj = data.get(i).isObject();
                final Category category = new Category(obj);
                final MenuItemBox box = new MenuItemBox(category);
                this.add(box);
                // make the widget draggable
                widgetDragController.makeDraggable(box);
            }
        }
    }
    public JSONArray getLiveData() {
        JSONArray data = new JSONArray();
        int i = 0;
        for (Widget box : this.getChildren()) {
            if (box instanceof MenuItemBox) {
                MenuItemBox miBox = (MenuItemBox)box;
                miBox.setMenuOrder(this.getWidgetIndex(miBox));
                data.set(i++, miBox.getCategory().getJSON());
            }
        }
        return data;
    }

    protected void sendLiveData() {
        new Requester().setCategoryAll(getLiveData());
    }

}
